package com.sjjy.ta.action;

import com.sjjy.ta.form.OperationLogForm;
import com.sjjy.ta.iface.OperationLogService;
import com.sys.plat.bean.PageBean;
import com.sys.plat.util.HttpUtil;
import com.sys.plat.web.core.ActionContext;
import com.sys.plat.web.core.BaseAction;
import org.apache.struts.upload.FormFile;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: mabo
 * Date: 11-3-14
 * Time: 下午8:00
 * To change this template use File | Settings | File Templates.
 */
public class OperationLogAction extends BaseAction {
    OperationLogForm getForm() {
        return (OperationLogForm) ActionContext.getForm();
    }

    OperationLogService getService() {
        return (OperationLogService) getService("OperationLogImpl");
    }


    public String list() {
        PageBean pb = createPageBean(getForm());
        getService().list(pb);
        pb.setToWeb();
        HttpUtil.reqSetAttr("fmap", getForm().getFilter());
        HttpUtil.saveSearchParemeter();
        return "list";
    }

    public String add() {
        HttpUtil.reqSetAttr("ol", getForm().getOl());
        return "edit";
    }

    public String update() {
        getService().save(getForm().getOl());

        FormFile file = getForm().getUpload();
        String fileName = file.getFileName();

        if (fileName.equals("")) {
            return "close";
        }

        String type = fileName.substring(fileName.indexOf(".") + 1, fileName.length());
        fileName = getForm().getOl().getId() + "." + type;
        getForm().getOl().setUpFile(fileName);


        if (!type.equals("jpg") && !type.equals("txt") && !type.equals("xls") && !type.equals("doc")) {
            HttpUtil.reqSetAttr("mes", "记录已保存，但文件类型不符，请上传jpg,txt,xls,doc文件，并重新保存。");
            return add();
        } else {
            String filePath = ActionContext.getRequest().getRealPath("")
                    + "\\upload\\";
            File uploadFile = new File(filePath);
            File uploadFile2 = new File("E:\\sjjy\\upload\\");
            if (!uploadFile.exists() || uploadFile == null) { //判断指定路径dir是否存在，不存在则创建路径
                uploadFile.mkdirs();
            }
            if (!uploadFile2.exists() || uploadFile2 == null) { //判断指定路径dir是否存在，不存在则创建路径
                uploadFile2.mkdirs();
            }
            //上传的路径+文件名
            String path = filePath + "\\" + fileName;
            InputStream streamIn = null;
            OutputStream streamOut = null,streamOut2 = null;
            try {
                streamIn = file.getInputStream();
                streamOut = new FileOutputStream(path);
                streamOut2 = new FileOutputStream("E:\\sjjy\\upload\\"+fileName);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
                    streamOut.write(buffer, 0, bytesRead);
                    streamOut2.write(buffer, 0, bytesRead);
                }
                streamOut.close();
                streamOut2.close();
                streamIn.close();
                file.destroy();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (streamOut != null) {
                        streamOut.close();
                        streamOut2.close();
                    }
                    if (streamIn != null) {
                        streamIn.close();
                    }
                } catch (IOException e) {
                }
            }
        }
        getService().save(getForm().getOl());
        return "close";
    }
}
